package com.example.system.service.impl;

import com.example.common.response.R;
import com.example.common.utils.StringUtils;
import com.example.system.domain.bo.LogBo;
import com.example.system.domain.bo.PageBo;
import com.example.system.domain.entity.Log;
import com.example.system.domain.vo.PageVo;
import com.example.system.mapper.LogMapper;
import com.example.system.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class LogServiceImpl implements LogService {
    @Autowired
    private LogMapper logMapper;

    /**
     * 获取日志列表-分页
     */
    @Override
    public PageVo getPageVo(PageBo pageBo) {
        List<Log> logs = this.getList();
        return new PageVo(pageBo, logs);

    }

    /**
     * 获取日志列表-不分页
     */
    public List<Log> getList() {
        return logMapper.getList();

    }

    /**
     * 搜索日志列表-分页
     */
    @Override
    public R<PageVo> getPageVoSearch(LogBo bo, PageBo pageBo) {
        List<Log> list = this.getList();
        String s = bo.getAccount();
        if (StringUtils.isEmpty(s)) {
            return R.ok(new PageVo(pageBo, list));
        }
        //账号，接口，地址，参数，方法
        list = list.stream().filter(o -> o.getAccount().contains(s) || o.getUri().contains(s) || o.getUrl().contains(s) || o.getParams().contains(s) || o.getMethod().contains(s)).collect(Collectors.toList());
        if (StringUtils.isNotEmpty(list)) {
            return R.ok(new PageVo(pageBo, list));
        }
        return R.info("没有符合条件的数据", new PageVo());
    }

    /**
     * 新增日志
     */
    @Async
    @Override
    public void insertLog(Log log) {
        logMapper.insertLog(log);
    }
}
